package mo.offer_special;

import java.util.Stack;

public class L036 {

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (isOperator(token)) {
                int value2 = stack.pop();
                int value1 = stack.pop();
                if ("+".equals(token)) {
                    stack.push(value1 + value2);
                } else if ("-".equals(token)) {
                    stack.push(value1 - value2);
                } else if ("*".equals(token)) {
                    stack.push(value1 * value2);
                } else {
                    stack.push(value1 / value2);
                }
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }

    private boolean isOperator(String token) {
        return "+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token);
    }
}
